//
//  TSDetailRecordAlertView.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/23.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSDetailRecordAlertViewDelegate : NSObjectProtocol {
    func tradeWillBeRevoke(id:String)
    func scanRecordDetail(model:TSRecordModel)
}

protocol TSDetailRecordAlertTableViewCellDelegate : NSObjectProtocol {
    func didClickRevoke(id:String)
}

class TSDetailRecordAlertView: UIView {
    
    weak var delegate : TSDetailRecordAlertViewDelegate? = nil
    var tableController : TSBaseTableViewController!
    var dataSource : [TSRecordModel] = [] {
        didSet {
            tableController.tableView.reloadData()
        }
    }
    
    lazy var sectionHeader : TSRecordAlertHeaderView = {
        var header = TSRecordAlertHeaderView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 30))
        header.backgroundColor = WHITE_COLOR
        return header
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(white: 0, alpha: 0.8)
        configViews()
    }
    
    @objc func dismissAction() {
        self.isHidden = true
        tableController.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: false)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: - TSDetailRecordAlertTableViewCellDelegate
extension TSDetailRecordAlertView : TSDetailRecordAlertTableViewCellDelegate {
    func didClickRevoke(id: String) {
        if self.delegate != nil {
            self.delegate?.tradeWillBeRevoke(id: id)
        }
    }
}

extension TSDetailRecordAlertView : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.delegate != nil {
            let model = dataSource[indexPath.row]
            self.delegate?.scanRecordDetail(model: model)
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        sectionHeader
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        30
    }
}

//MARK: UITableViewDataSource
extension TSDetailRecordAlertView : UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSDetailRecordAlertTableViewCell.self)) as? TSDetailRecordAlertTableViewCell
        
        if cell == nil {
            cell = TSDetailRecordAlertTableViewCell.init(style: .default, reuseIdentifier:
                NSStringFromClass(TSDetailRecordAlertTableViewCell.self))
        }
        
        cell?.delegate = self
            
        cell?.setupCell(model: dataSource[indexPath.row])
        
        return cell!
    }
 
}

extension TSDetailRecordAlertView {
    
    func configViews() {
        
        let container = UIView.init()
        container.backgroundColor = WHITE_COLOR
        container.layer.cornerRadius = 10
        container.layer.masksToBounds = true
        addSubview(container)
        container.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(15)
            make.centerX.centerY.equalToSuperview()
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = "投注记录"
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
        }
        
        let cellH : CGFloat = 30
        let estimateTableViewH = SCREEN_HEIGHT * 0.65
        let maxCount = floor(estimateTableViewH / cellH)
        
        tableController = TSBaseTableViewController(style: .plain)
        tableController.emptyAlertText = "暂无记录"
        tableController.tableView.backgroundColor = WHITE_COLOR
        tableController.tableView.delegate = self;
        tableController.tableView.dataSource = self
        tableController.tableView.separatorStyle = .none
        tableController.tableView.bounces = false
        container.addSubview(tableController.tableView)
        tableController.tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.height.equalTo(maxCount*cellH)
        }
        
        let dismissBtn = UIButton.init(type: .system)
        dismissBtn.setTitle("关闭", for: .normal)
        dismissBtn.setTitleColor(WHITE_COLOR, for: .normal)
        dismissBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        dismissBtn.backgroundColor = BASE_RED_COLOR
        dismissBtn.layer.cornerRadius = 20
        dismissBtn.layer.masksToBounds = true
        dismissBtn.addTarget(self, action: #selector(dismissAction), for: .touchUpInside)
        container.addSubview(dismissBtn)
        dismissBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(tableController.tableView.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: (SCREEN_WIDTH - 2*15)*0.3, height: 40))
            
            make.bottom.equalTo(-10)
        }
    }
}

//MARK: - TSDetailRecordAlertTableViewCell
class TSDetailRecordAlertTableViewCell: UITableViewCell {
    
    weak var delegate : TSDetailRecordAlertTableViewCellDelegate? = nil
    var periodLabel : UILabel!
    var timesLabel : UILabel!
    var stateLabel : UILabel!
    var revokeBtn : UIButton!
    var model : TSRecordModel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configViews()
    }
    
    func setupCell(model:TSRecordModel) {
        self.model = model
        
        periodLabel.text = "\(model.qihao)"
        timesLabel.text = "\(model.jizhu)"
        revokeBtn.setTitle(model.status == 0 ? "撤单" : "-", for: .normal)
        revokeBtn.isEnabled = model.status == 0
        
        switch model.status {
        case 0:
            stateLabel.text = "未开奖"
        case 1:
            stateLabel.text = "中奖"
        case 2:
            stateLabel.text = "未中奖"
        case 3:
            stateLabel.text = "撤单"
        default:break
        }
    }
    
    @objc func revokeAction() {
        
        if self.delegate != nil {
            self.delegate?.didClickRevoke(id: "\(model.id)")
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension TSDetailRecordAlertTableViewCell {
    
    func configViews() {
        
        periodLabel = UILabel.init()
        periodLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        periodLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        periodLabel.textAlignment = .center
        periodLabel.adjustsFontSizeToFitWidth = true
        periodLabel.minimumScaleFactor = 0.6
        contentView.addSubview(periodLabel)
        
        timesLabel = UILabel.init()
        timesLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        timesLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        timesLabel.textAlignment = .center
        timesLabel.adjustsFontSizeToFitWidth = true
        timesLabel.minimumScaleFactor = 0.6
        contentView.addSubview(timesLabel)
        
        stateLabel = UILabel.init()
        stateLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        stateLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        stateLabel.textAlignment = .center
        stateLabel.adjustsFontSizeToFitWidth = true
        stateLabel.minimumScaleFactor = 0.6
        contentView.addSubview(stateLabel)
        
        revokeBtn = UIButton.init(type: .system)
        revokeBtn.setTitleColor(BASE_RED_COLOR, for: .normal)
        revokeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        revokeBtn.addTarget(self, action: #selector(revokeAction), for: .touchUpInside)
        contentView.addSubview(revokeBtn)
        
        let array = [periodLabel, timesLabel, stateLabel, revokeBtn]
        array.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 0, leadSpacing: 0, tailSpacing: 0)
        array.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
        }
        
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#DDDDDD")
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
}

class TSRecordAlertHeaderView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configViews() {
        
        let periodLabel = UILabel.init()
        periodLabel.text = "期号"
        periodLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        periodLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        periodLabel.textAlignment = .center
        periodLabel.adjustsFontSizeToFitWidth = true
        periodLabel.minimumScaleFactor = 0.6
        addSubview(periodLabel)
        
        let timesLabel = UILabel.init()
        timesLabel.text = "倍数"
        timesLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        timesLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        timesLabel.textAlignment = .center
        timesLabel.adjustsFontSizeToFitWidth = true
        timesLabel.minimumScaleFactor = 0.6
        addSubview(timesLabel)
        
        let stateLabel = UILabel.init()
        stateLabel.text = "状态"
        stateLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        stateLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        stateLabel.textAlignment = .center
        stateLabel.adjustsFontSizeToFitWidth = true
        stateLabel.minimumScaleFactor = 0.6
        addSubview(stateLabel)
        
        let revokeLabel = UILabel.init()
        revokeLabel.text = "操作"
        revokeLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        revokeLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        revokeLabel.textAlignment = .center
        revokeLabel.adjustsFontSizeToFitWidth = true
        revokeLabel.minimumScaleFactor = 0.6
        addSubview(revokeLabel)
        
        let array = [periodLabel, timesLabel, stateLabel, revokeLabel]
        array.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 0, leadSpacing: 0, tailSpacing: 0)
        array.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
        }
        
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#DDDDDD")
        addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
}
